﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericLibrariesToolKit
{
    /// <summary>
    /// DBUtilFactory类，用于创建DBUtil
    /// </summary>
    public class DBUtilFactory
    {
        //定义一个数据源连接字符串
        private static string dataSource;

        //单列模式实现代码
        #region
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private DBUtilFactory() { }

        /// <summary>
        /// 创建一个DBUnitFactory对象
        /// </summary>
        /// <param name="inputDataSource">数据源</param>
        /// <returns>DBUtilFactory对象</returns>
        public static DBUtilFactory CreateDBUtilFactory(string inputDataSource)
        {
            //初始化数据源
            dataSource = inputDataSource;
            //返回DBUnitFactory实例
            return DBUtilFactoryHolder.instance;
        }

        /// <summary>
        /// 类级内部类
        /// </summary>
        private static class DBUtilFactoryHolder
        {
            //静态初始化器
            public static DBUtilFactory instance = new DBUtilFactory();
        }
        #endregion

        /// <summary>
        /// 创建一个DBUtil对象
        /// </summary>
        /// <returns>DBUtil对象</returns>
        public DBUtil CreateDBUtil()
        {
            //配置DBUnit的数据源
            DBUtil.dataSource = dataSource;
            return DBUtil.CreateDBUtil();
        }
    }
}

